﻿Imports System.Drawing.Drawing2D

Public Class Box : Inherits BaseControl

	Public WithEvents NotesPanel As Panel
	Public WithEvents ToolStrip As ToolStrip

	Public Sub New()
		BackColor = Themes.Current.FormColor
		ToolStrip = New ToolStrip
		Themes.Register(ToolStrip)
		ToolStrip.Dock = DockStyle.Top
		Controls.Add(ToolStrip)
		ToolStrip.Visible = False
		SearchHost = New ToolStripControlHost(Find)
		ToolStrip.Items.Add(SearchHost)
		SearchHost.Alignment = ToolStripItemAlignment.Right
		SearchHost.AutoSize = True
		SearchHost.BackColor = Color.Transparent
		Find.Width = 160
	End Sub

	Dim SearchHost As ToolStripControlHost
	Property SearchVisible As Boolean
		Get
			Return SearchHost.Visible
		End Get
		Set(value As Boolean)
			SearchHost.Visible = value
		End Set
	End Property

	Sub AddButton(Name As String, Text As String, Optional Image As Image = Nothing)
		ToolStrip.AddButton(Name, Text, Image)
		ToolStrip.Visible = True
	End Sub

	Sub AddButtonMenu(Parent As String, Name As String, Text As String, Optional Image As Image = Nothing, Optional ShortcutKeys As Keys = Nothing)
		ToolStrip.AddButtonMenu(Parent, Name, Text, Image)
	End Sub

	Sub ApplyFilters()
		For Each Control In Controls
			If TypeOf Control Is Report Then CType(Control, Report).Restart()
		Next
	End Sub

	Sub AddButtons(Items As String)
		For Each Item In Items.Split(".")
			Select Case Item
				Case "Add"
					AddButton("Add", "Ekle", My.Resources.add.Resize(16, 16, 2))
				Case "Edit"
					AddButton("Edit", "Düzenle", My.Resources.editing)
				Case "Delete"
					AddButton("Delete", "Sil")
				Case "Print"
					AddButton("Print", "Yazdır", My.Resources.print)
				Case "-"
					ToolStrip.Items.Add("-")
				Case Else
					Dim Data = Item.Split("|")
					AddButton(Data(0), Data(1))
			End Select
		Next
	End Sub

	ReadOnly Property Buttons(Name As String) As ToolStripItem
		Get
			Return ToolStrip.Items(Name)
		End Get
	End Property

	ReadOnly Property Buttons(Name As String, Menu As String) As ToolStripMenuItem
		Get
			Return CType(ToolStrip.Items(Name), ToolStripDropDownItem).DropDownItems(Name & "." & Menu)
		End Get
	End Property

	Dim Notes As New List(Of String)

	Public Sub AddNote(ByVal Icon As String, ByVal Text As String)
		If Panels(1) Is Nothing Then
			TopPanel.Height = 0
			NotesPanel = TopPanel
		End If
		Notes.Add(Icon & Text)
		If NotesPanel.Height = 0 Then NotesPanel.Height += 8
		NotesPanel.Height += 19
		NotesPanel.Visible = True
		NotesPanel.Invalidate()
	End Sub

	Sub ClearNotes()
		Notes.Clear()
	End Sub

	Private Sub NotesPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles NotesPanel.Paint
		Dim y As Integer = 4
		Dim G As Graphics = e.Graphics
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		G.InterpolationMode = InterpolationMode.HighQualityBilinear
		For Each Note As String In Notes
			Dim R = New Rectangle(6, y + 2, 15, 15)
			Select Case Mid(Note, 1, 1)
				Case "i" : G.DrawImage(My.Resources.Info, R)
				Case "!" : G.DrawImage(My.Resources.warning, R)
				Case "x" : R.Inflate(-2, -2) : G.DrawImage(My.Resources.ierror, R)
			End Select
			R = New Rectangle(23, y, Width - 21, 19)
			DrawStringLighted(G, Mid(Note, 2), Font, Color.Black, R, StrFormat("l", "c"))
			y += 19
		Next
	End Sub

	Dim Panels(3) As Panel
	ReadOnly Property BottomPanel As Panel
		Get
			If Panels(3) Is Nothing Then
				Panels(3) = New Panel
				Panels(3).Dock = DockStyle.Bottom
				Controls.Add(Panels(3))
				Panels(3).BringToFront()
			End If
			Return Panels(3)
		End Get
	End Property

	ReadOnly Property LeftPanel As Panel
		Get
			If Panels(0) Is Nothing Then
				Panels(0) = New Panel
				Panels(0).Dock = DockStyle.Left
				Controls.Add(Panels(0))
				Panels(0).SendToBack()
				ToolStrip.SendToBack()
			End If
			Return Panels(0)
		End Get
	End Property

	ReadOnly Property TopPanel As Panel
		Get
			If Panels(1) Is Nothing Then
				Panels(1) = New Panel
				Panels(1).Dock = DockStyle.Top
				Controls.Add(Panels(1))
				Panels(1).SendToBack()
				ToolStrip.SendToBack()
			End If
			Return Panels(1)
		End Get
	End Property

	Dim WithEvents Find As New FindBox

	Sub ShowFind()
		ToolStrip.Visible = True
		Find.Controls(0).Focus()
	End Sub

	Private Sub Find_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Find.TextChanged
		If TypeOf Controls(0) Is Grid Then
			CType(Controls(0), Grid).Search(Find.FindText)
		End If
	End Sub


	Private Sub ToolStrip_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip.ItemClicked
		If TypeOf e.ClickedItem Is ToolStripSplitButton Then
			If CType(e.ClickedItem, ToolStripSplitButton).DropDownButtonPressed Then Return
		End If
		RaiseActionEvent(e.ClickedItem.Name)
	End Sub

	Sub PerformClick(Button As String)
		ToolStrip.Items(Button).PerformClick()
	End Sub

	ReadOnly Property Content As Control
		Get
			Return (From c As Control In Controls Where c.Dock = DockStyle.Fill).FirstOrDefault
		End Get
	End Property

	Private Sub Find_SearchEnded() Handles Find.SearchEnded
		If Content Is Nothing Then Return
		Content.Focus()
	End Sub

	Public Overrides Sub Reset()
		ClearNotes()
		For Each Control In Controls
			If TypeOf Control Is Grid Then CType(Control, Grid).Reset()
		Next
		If HasFilter() Then Filter.Reset()
		If HasLeftBar Then LeftBar.Reset()
	End Sub

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Back Then
			'If Not Grid Is Nothing AndAlso Grid.Focused Then
			Find.Reset()
			'End If
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Dim WithEvents _LeftBar As LeftBar
	ReadOnly Property LeftBar As LeftBar
		Get
			If _LeftBar Is Nothing Then
				_LeftBar = New LeftBar
				Controls.Add(_LeftBar)
				_LeftBar.Dock = DockStyle.Left
				_LeftBar.Width = 180
				_LeftBar.SendToBack()
				ToolStrip.SendToBack()
				_LeftBar.Visible = True
			End If
			Return _LeftBar
		End Get
	End Property

	Private Sub LeftBar_TabChanged() Handles _LeftBar.TabChanged
		If Not Process Is Nothing Then Process.GenericEvent(New ProcessEventArgs(EventType.TabSelected))
	End Sub

	Dim WithEvents _Filter As Filter
	ReadOnly Property Filter As Filter
		Get
			If _Filter Is Nothing Then
				_Filter = New Filter
				Controls.Add(_Filter)
				_Filter.Dock = DockStyle.Left
				_Filter.Width = 180
				_Filter.SendToBack()
				ToolStrip.SendToBack()
				_Filter.Visible = True
			End If
			Return _Filter
		End Get
	End Property

	Function HasFilter() As Boolean
		Return Not _Filter Is Nothing
	End Function

	Function HasLeftBar() As Boolean
		Return Not _LeftBar Is Nothing
	End Function

End Class

Public Class FindBox : Inherits UserControl

	Dim WithEvents TextBox As New TextBox

	Sub New()
		Controls.Add(TextBox)
		BackColor = Color.Transparent
		TextBox.DefaultText = "Listede ara (Ctrl+F)"
		TextBox.BackColor = Color.White
		TextBox.Location = New Point(2, 2)
		TextBox.Width = Width - TextBox.Height - 2
		MaximumSize = New Size(1000, TextBox.Height + 4)
		MinimumSize = New Size(1, TextBox.Height + 4)
		TextBox.Anchor += AnchorStyles.Right
		BackColor = Color.White
		DoubleBuffered = True
	End Sub

	Property FindText As String
		Get
			Return TextBox.Text
		End Get
		Set(value As String)
			TextBox.Text = value
		End Set
	End Property

	Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		Dim r As New Rectangle(0, 0, Width - 1, Height - 1)
		'r.Inflate(-2, -2)
		e.Graphics.SmoothingMode = SmoothingMode.HighQuality
		e.Graphics.FillRoundRect(r, Brushes.White, 6)
		e.Graphics.DrawRoundRect(r, Color.Black.Opacity(64).Pen, 6)
		r = New Rectangle(Width - Height, 0, Height, Height)
		r.Width = r.Height
		r.Inflate(-5, -5)
		e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear
		e.Graphics.DrawImage(My.Resources.bwsearch, r)
	End Sub

	Event SearchEnded()

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Escape Then
			TextBox.Text = ""
			RaiseEvent SearchEnded()
			Return True
		ElseIf keyData = Keys.Tab Then
			RaiseEvent SearchEnded()
			Return True
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Private Sub TextBox_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox.KeyDown
		If e.KeyCode = Keys.Down Then
			e.Handled = True
			RaiseEvent SearchEnded()
		End If
	End Sub

	Private Sub TextBox_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox.TextChanged
		OnTextChanged(Nothing)
	End Sub

	Sub Reset()
		TextBox.Text = ""
	End Sub


End Class